package org.wabson.alfresco.translations;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

public class FileSystemLoader implements BundleLoader
{
    /**
     * Base location
     */
    private String basePath;

    /**
     * Instantiate a new file system message loader with the given base location
     * 
     * @param fileName
     */
    public FileSystemLoader(String basePath)
    {
        this.basePath = basePath;
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#exists(java.lang.String)
     */
    public boolean exists(String filePath)
    {
        File f = new File(basePath, filePath);
        return f.exists();
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#load(java.lang.String)
     */
    public MessageBundle load(String filePath) throws BundleLoadingException
    {
        MessageBundle mb = new MessageBundle(filePath);
        
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(basePath
                    + File.separator + filePath));
    
            String s;
            while ((s = in.readLine()) != null)
            {
                mb.addLine(s);
            }
        }
        catch (IOException e)
        {
            throw new BundleLoadingException(e);
        }
        return mb;
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#findBundleFiles()
     */
    public Set<String> findBundleFiles()
    {
        return findBundleFiles(basePath, basePath, true);
    }

    /* (non-Javadoc)
     * @see org.wabson.alfresco.translations.BundleLoader#findBundleFiles(java.lang.String, java.lang.String, boolean)
     */
    public Set<String> findBundleFiles(String filePath, String basePath,
            boolean recurse)
    {
        Set<String> bundles = new TreeSet<String>();

        File f = new File(filePath);
        File bf = new File(basePath);

        if (f.isDirectory())
        {
            for (String child : f.list())
            {
                File cf = new File(f, child);
                if (cf.isFile() && PROPERTIES_FILE_PATTERN.matcher(child).matches())
                {
                    String relPath = cf.getPath()
                            .substring(
                                    bf.getPath().length()
                                            + File.pathSeparator.length());
                    bundles.add(relPath.replace('\\', '/'));
                } else if (cf.isDirectory() && recurse)
                {
                    bundles.addAll(findBundleFiles(cf.getPath(), basePath,
                            recurse));
                }
            }
        } else if (f.isFile())
        {
            bundles.add("");
        }

        return bundles;
    }
    
    public String toString()
    {
        return "file:" + getBasePath();
    }

    public String getBasePath()
    {
        return basePath;
    }

	@Override
	public String getFileName()
	{
		return basePath.lastIndexOf("/") > -1 ? basePath.substring(basePath.lastIndexOf("/") + 1) : basePath;
	}

	@Override
	public int getNumFiles()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Set<String> getFiles()
	{
		// TODO Auto-generated method stub
		return null;
	}
}
